package com.example.firstapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.service.fileService;

public class fileActivety extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.file);
		
		saveFile();
		
		readFile();
	}

	private void saveFile() {
		Button button = (Button)this.findViewById(R.id.Button);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText fileName = (EditText)findViewById(R.id.fileName);
				String fileNameText = fileName.getText().toString();
				
				EditText fileContext = (EditText)findViewById(R.id.fileContext);
				String fileContextText = fileContext.getText().toString();
				
				fileService service = new fileService(getApplicationContext());
				try {
					service.saveFile(fileNameText, fileContextText);
					Toast toast=Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT);
					toast.show();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Toast toast=Toast.makeText(getApplicationContext(), "failure", Toast.LENGTH_SHORT);
					toast.show();
				}
			}
		});
	}
	
	
	private void readFile() {
		Button button = (Button)this.findViewById(R.id.display);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText fileName = (EditText)findViewById(R.id.fileName);
				String fileNameText = fileName.getText().toString();
				
				EditText fileContext = (EditText)findViewById(R.id.fileContext);
				
				fileService service = new fileService(getApplicationContext());
				try {
					String fileContextText = service.readFile(fileNameText);
					fileContext.setText(fileContextText);
					Toast toast=Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT);
					toast.show();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Toast toast=Toast.makeText(getApplicationContext(), "failure", Toast.LENGTH_SHORT);
					toast.show();
				}
			}
		});
	}

}
